﻿#include "CountDownWidget.h"
#include <QFile>
#include <QDebug>

#define MAXTIME 3600


CountDownWidget::CountDownWidget(QWidget *parent)
    : QWidget(parent)
    , m_iSumTime(300)
    , m_iMinute(5)
    , m_iSecond(0)
    , m_pTimer(nullptr)
{
    InitUi();

    InitProperty();
}

CountDownWidget::~CountDownWidget()
{
    if(m_pTimer->isActive())
    {
        m_pTimer->stop();
    }
}

int CountDownWidget::GetSumTime() const
{
    return m_iSumTime;
}

void CountDownWidget::InitUi()
{
    m_pRootLayout = new QHBoxLayout(this);
    m_pRootLayout->setContentsMargins(0,0,0,0);
    m_pRootLayout->setSpacing(0);

    m_pDecadeMinute = new NixieTube(this);
    m_pDecadeMinute->SetMaxNum(5);
    m_pTheUnitMinute = new NixieTube(this);
    m_pTheUnitMinute->SetMaxNum(9);

    m_pColonWidget = new QWidget(this);
    m_pColonWidget->setObjectName("ColonWidget");
    QVBoxLayout *m_pColonWidgetLayout = new QVBoxLayout(m_pColonWidget);
    m_pColonWidgetLayout->setContentsMargins(0,0,0,0);
    m_pColonWidgetLayout->setSpacing(10);
    m_pColonWidgetLayout->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);

    m_pTopColon = new QWidget(m_pColonWidget);
    m_pTopColon->setObjectName("TopColon");
    m_pBottomColon = new QWidget(m_pColonWidget);
    m_pBottomColon->setObjectName("BottomColon");

    m_pColonWidgetLayout->addWidget(m_pTopColon);
    m_pColonWidgetLayout->addWidget(m_pBottomColon);

    m_pDecadeSecond = new NixieTube(this);
    m_pDecadeSecond->SetMaxNum(5);
    m_pTheUnitSecond = new NixieTube(this);
    m_pTheUnitSecond->SetMaxNum(9);

    m_pRootLayout->addWidget(m_pDecadeMinute);
    m_pRootLayout->addWidget(m_pTheUnitMinute);
    m_pRootLayout->addWidget(m_pColonWidget);
    m_pRootLayout->addWidget(m_pDecadeSecond);
    m_pRootLayout->addWidget(m_pTheUnitSecond);
}

void CountDownWidget::InitProperty()
{
    this->setWindowFlags(Qt::FramelessWindowHint);
    setAutoFillBackground(true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background,Qt::transparent);
    setPalette(pal);
    setWindowOpacity(1);
    setAttribute(Qt::WA_TranslucentBackground);

    //this->setMinimumSize(QSize(500, 300));

    QFile resourceqss(":/qss/CountDownWidget.qss");
    resourceqss.open(QFile::ReadOnly);
    this->setStyleSheet(resourceqss.readAll());
    resourceqss.close();

    connect(m_pDecadeMinute, &NixieTube::currentnumed, this, [=](int num){
        //m_iMinute = m_iMinute%10;
        //m_iSumTime = (num * 10 * 60) +
        m_iMinute = (num * 10) + (m_iMinute%10);
        m_iSumTime = m_iMinute * 60 + m_iSecond;
        //qDebug() << "SumTime: " << m_iSumTime%60;
    });

    connect(m_pTheUnitMinute, &NixieTube::currentnumed, this, [=](int num){
        m_iMinute = (m_iMinute/10)*10 + num;
        m_iSumTime = m_iMinute * 60 + m_iSecond;
        //qDebug() << "SumTime: " << m_iSumTime;
    });

    connect(m_pDecadeSecond, &NixieTube::currentnumed, this, [=](int num){
        m_iSecond = (num * 10) + (m_iSecond%10);
        m_iSumTime = m_iMinute * 60 + m_iSecond;
        //qDebug() << "SumTime: " << m_iSumTime;
    });

    connect(m_pTheUnitSecond, &NixieTube::currentnumed, this, [=](int num){
        m_iSecond = (m_iSecond/10)*10 + num;
        m_iSumTime = m_iMinute * 60 + m_iSecond;
        //qDebug() << "SumTime: " << m_iSumTime;
    });

    //connect(this, &CountDownWidget::reset, m_pDecadeMinute, &NixieTube::on_reset);
    //connect(this, &CountDownWidget::reset, m_pTheUnitMinute, &NixieTube::on_reset);
    //connect(this, &CountDownWidget::reset, m_pDecadeSecond, &NixieTube::on_reset);
    //connect(this, &CountDownWidget::reset, m_pTheUnitSecond, &NixieTube::on_reset);

    m_pTimer = new QTimer(this);

    connect(m_pTimer, &QTimer::timeout, this, [=](){

        if(m_iSumTime <= 0)
        {
            m_pTimer->stop();
            emit timeout();
            return;
        }
        m_iSumTime--;
        emit updatetime(m_iSumTime);
        UpdateTimer(m_iSumTime);
        if(m_iSumTime <= 0)
        {
            m_pTimer->stop();
            emit timeout();
            return;
        }

    });

    on_reset();

}

void CountDownWidget::UpdateTimer(int timer)
{
    m_iMinute = timer/60;
    m_iSecond = timer%60;
    m_pDecadeMinute->SetCurrentNum(m_iMinute/10);
    m_pTheUnitMinute->SetCurrentNum(m_iMinute%10);
    m_pDecadeSecond->SetCurrentNum(m_iSecond/10);
    m_pTheUnitSecond->SetCurrentNum(m_iSecond%10);
}

void CountDownWidget::on_start(bool start, bool pause)
{
    if(start)
    {
        m_pTimer->start(1000);
    } else {
        m_pTimer->stop();
    }
    if(!pause)
    {
        m_pDecadeMinute->SetButtonVisible(!start);
        m_pTheUnitMinute->SetButtonVisible(!start);
        m_pDecadeSecond->SetButtonVisible(!start);
        m_pTheUnitSecond->SetButtonVisible(!start);
    }

}

void CountDownWidget::on_reset()
{
    m_iSumTime = 300;
    m_iMinute = 5;
    m_iSecond = 0;
    m_pDecadeMinute->SetCurrentNum(0);
    m_pTheUnitMinute->SetCurrentNum(5);
    m_pDecadeSecond->SetCurrentNum(0);
    m_pTheUnitSecond->SetCurrentNum(0);
    //emit reset();
}
